package com.zeildx.glife.view;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zeildx.glife.Adapter;
import com.zeildx.glife.Event;
import com.zeildx.glife.EventDao;
import com.zeildx.glife.LifeViewModel;
import com.zeildx.glife.R;
import com.zeildx.glife.database.WordDatabase;
import com.zeildx.glife.entity.Word;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link lifeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class lifeFragment extends Fragment {
    Button button_Insert,button_Delete;
    LifeViewModel lifeViewModel;
    public RecyclerView life_recyclerView;
    Adapter adapter;
    private View view;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public lifeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment lifeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static lifeFragment newInstance(String param1, String param2) {
        lifeFragment fragment = new lifeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


    }
    @Override
    public void onActivityCreated(@Nullable  Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        requireActivity().setContentView(R.layout.fragment_life);
        life_recyclerView = requireActivity().findViewById(R.id.life_recyclerView);
        adapter = new Adapter();
        life_recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
        life_recyclerView.setAdapter(adapter);
        lifeViewModel=ViewModelProviders.of(this).get(LifeViewModel.class);

        lifeViewModel.getAllEventsLive().observe(getViewLifecycleOwner(), new Observer<List<Event>>() {
            @Override
            public void onChanged(List<Event> events) {
                adapter.setAllEvents(events);
                adapter.notifyDataSetChanged();

            }
        });
        button_Insert = requireActivity().findViewById(R.id.button_Insert);
        button_Delete = requireActivity().findViewById(R.id.button_Delete);
        button_Insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] event = {
                        "创新创业比赛",
                        "ACM国际大学生程序设计竞赛",
                        "Topcoder竞赛",
                        "百度之星程序设计大赛",
                        "华为软件精英挑战赛",
                        "全国大学生数学建模大赛"
                };
                for(int i=0;i<event.length;i++){
                    lifeViewModel.insertEvents(new Event(event[i]));
                }
            }
        });
        button_Delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Event event1 = new Event("创新创业比赛");
                event1.setId(1);
                lifeViewModel.deleteEvents(event1);
            }
        });



    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_life, container, false);

    }

}
